<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/7/2
 * Time: 13:53
 */
namespace app\orders\controller;

use app\common\ErrorCodes;
use think\Lang;
class Program extends Base{
    public function index(){
//        $this->authVerify();
        $field = 'pro_id,pro_name,create_time,connect_way,pro_pay';
        $programs = $this->programs()->page($field, $where='', $order='');
        $programs = $this->resetPros($programs);
        $this->page($programs);
        $this->assign('programs', $programs);
        return $this->show($programs);
//        return $this->fetch("pro/index");
    }

    /**
     * 添加action
     */
    public function save(){
//        $this->authVerify();
        $programs = input('post.');
        $programs['create_time'] = date('Y-m-d H:i:s', time());
        $validate = $this->validate($programs, 'Pros');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->programs()->add($programs, '');
        if(!$result){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('add fail'));
//            return $this->error(Lang::get('add fail'));
        }
        return $this->show($programs, ErrorCodes::$SUCCESS, Lang::get('add success'));
//        return $this->success(Lang::get('add success'), 'Program/index');
    }

    /**
     * json数据
     */
    public function one(){
        $pro_id = input('param.pro_id');
        $field = 'pro_id,pro_name,create_time,connect_way,pro_pay';
        $where = array('pro_id'=>$pro_id);
        $data = $this->programs()->select($field, $where);
        $data = $this->resetPros($data);
        return $this->show($data);
    }

    /**
     * 更新action
     */
    public function esave(){
        $this->authVerify();
        $pro_id = input('param.pro_id');
        $where = array('pro_id'=>$pro_id);
        $find = $this->programs()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind program'));
//            return $this->error(Lang::get('unfind program'));
        }
        $programs = input('post.');
        $validate = $this->validate($programs, 'Pros');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->programs()->update($programs, $where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('edit fail'));
//            return $this->error(Lang::get('edit fail'));
        }
        return $this->show($programs, ErrorCodes::$SUCCESS, Lang::get('edit success'));
//        return $this->success(Lang::get('edit success'), 'Program/index');
    }

    /**
     * 删除action
     */
    public function del(){
        $this->authVerify();
        $pro_id = input('param.pro_id');
        $where = array('pro_id'=>$pro_id);
        $find = $this->programs()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind program'));
//            return $this->error(Lang::get('unfind program'));
        }
        $result = $this->programs()->del($where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del fail'));
//            return $this->error(Lang::get('del fail'));
        }
        return $this->show($find,ErrorCodes::$SUCCESS, Lang::get('del success'));
//        return $this->success(Lang::get('del success'), 'Program/index');
    }

    /**
     * 简单的模糊搜索
     * @return mixed
     */
    public function search(){
        $search = input('param.search');
        $data = $this->programs()->searchLike($search);
        $data = $this->resetPros($data);
        $this->page($data);
        $this->assign('programs', $data);
        return $this->show($data);
//        return $this->fetch('pro/index');
    }

    /**
     * 重组-二维数组
     * @param $programs
     * @return mixed
     */
    private function resetPros($programs){
        $len = count($programs);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $programs[$i] = $this->resetPro($programs[$i]);
            }
        }
        return $programs;
    }

    /**
     * 重组-一维数组
     * @param $program
     */
    private function resetPro($program){
        $len = count($program);
        if($len > 0){
            $program['connect'] = $this->switchWay($program['connect_way']);
            $program['pay'] = $this->switchPay($program['pro_pay']);
        }else{
            $program['connect'] = '';
            $program['pay'] = '';
        }
        return $program;
    }

    /**
     * 通讯方式
     * @param $connectWay
     * @return string
     */
    private function switchWay($connectWay){
        switch ($connectWay){
            case 0:
                $connect = 'RFID_V5';
                break;
            case 1:
                $connect = 'STS_RFID';
                break;
            case 2:
                $connect = 'STS_IR';
                break;
            case 3:
                $connect = 'STS_Lora';
                break;
            case 4:
                $connect = 'STS_NBIot';
                break;
            case 5:
                $connect = 'STS_GPRS';
                break;
            default:
                $connect = '';
                break;

        }
        return $connect;
    }

    /**
     * 付费方式
     * @param $proPay
     */
    private function switchPay($proPay){
        switch ($proPay){
            case 0:
                $pay = '预付费';
                break;
            case 1:
                $pay = '后付费';
                break;
            default:
                $pay = '';
                break;

        }
        return $pay;
    }
}